package com.ruoyi.busi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.busi.domain.bo.BusiCompanyEletricityBo;
import com.ruoyi.busi.domain.vo.BusiCompanyEletricityVo;
import com.ruoyi.busi.domain.BusiCompanyEletricity;
import com.ruoyi.busi.mapper.BusiCompanyEletricityMapper;
import com.ruoyi.busi.service.IBusiCompanyEletricityService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 电网排放系数Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-02
 */
@Service
public class BusiCompanyEletricityServiceImpl extends ServicePlusImpl<BusiCompanyEletricityMapper, BusiCompanyEletricity, BusiCompanyEletricityVo> implements IBusiCompanyEletricityService {

    @Override
    public BusiCompanyEletricityVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<BusiCompanyEletricityVo> queryPageList(BusiCompanyEletricityBo bo) {
        PagePlus<BusiCompanyEletricity, BusiCompanyEletricityVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<BusiCompanyEletricityVo> queryList(BusiCompanyEletricityBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<BusiCompanyEletricity> buildQueryWrapper(BusiCompanyEletricityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusiCompanyEletricity> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSb1()), BusiCompanyEletricity::getSb1, bo.getSb1());
        lqw.eq(StringUtils.isNotBlank(bo.getSb2()), BusiCompanyEletricity::getSb2, bo.getSb2());
        lqw.eq(StringUtils.isNotBlank(bo.getSb3()), BusiCompanyEletricity::getSb3, bo.getSb3());
        lqw.eq(StringUtils.isNotBlank(bo.getSb4()), BusiCompanyEletricity::getSb4, bo.getSb4());
        lqw.eq(StringUtils.isNotBlank(bo.getSb5()), BusiCompanyEletricity::getSb5, bo.getSb5());
        lqw.eq(StringUtils.isNotBlank(bo.getSb6()), BusiCompanyEletricity::getSb6, bo.getSb6());
        lqw.eq(StringUtils.isNotBlank(bo.getSb7()), BusiCompanyEletricity::getSb7, bo.getSb7());
        lqw.eq(StringUtils.isNotBlank(bo.getSb8()), BusiCompanyEletricity::getSb8, bo.getSb8());
        lqw.eq(StringUtils.isNotBlank(bo.getSb9()), BusiCompanyEletricity::getSb9, bo.getSb9());
        lqw.eq(StringUtils.isNotBlank(bo.getSb10()), BusiCompanyEletricity::getSb10, bo.getSb10());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BusiCompanyEletricityBo bo) {
        BusiCompanyEletricity add = BeanUtil.toBean(bo, BusiCompanyEletricity.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BusiCompanyEletricityBo bo) {
        BusiCompanyEletricity update = BeanUtil.toBean(bo, BusiCompanyEletricity.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BusiCompanyEletricity entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
